package sa.com.sure.ejtimaa.sync;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import sa.com.sure.ejtimaa.R;
import sa.com.sure.ejtimaa.R.style;
import sa.com.sure.ejtimaa.common.AppDialogs;
import sa.com.sure.ejtimaa.common.Helper;
import sa.com.sure.ejtimaa.database.MeetingAttachments;
import sa.com.sure.ejtimaa.database.MeetingAttendees;
import sa.com.sure.ejtimaa.database.MeetingItems;
import sa.com.sure.ejtimaa.database.MeetingItemsAttachments;
import sa.com.sure.ejtimaa.database.MeetingPlaces;
import sa.com.sure.ejtimaa.database.MeetingSessions;
import sa.com.sure.ejtimaa.database.MeetingStatuses;
import sa.com.sure.ejtimaa.database.Meetings;
import sa.com.sure.ejtimaa.database.Members;
import sa.com.sure.ejtimaa.database.ReplyTypes;
import sa.com.sure.ejtimaa.model.MeetingAttachmentInfo;
import sa.com.sure.ejtimaa.model.MeetingAttendeeInfo;
import sa.com.sure.ejtimaa.model.MeetingInfo;
import sa.com.sure.ejtimaa.model.MeetingItemInfo;
import sa.com.sure.ejtimaa.model.MeetingItemsAttachmentInfo;
import sa.com.sure.ejtimaa.model.MeetingPlaceInfo;
import sa.com.sure.ejtimaa.model.MeetingSessionInfo;
import sa.com.sure.ejtimaa.model.MeetingStatusInfo;
import sa.com.sure.ejtimaa.model.ReplyTypeInfo;
import sa.com.sure.ejtimaa.model.MemberInfo;
import sa.com.sure.ejtimaa.webservice.DataWebService;

public class SyncData {

	 ProgressDialog dialog;
	 int increment = 1;
	 int max = 9;
	public static SyncData Instance = new SyncData();
	
	public void SyncMeetingPlaces() throws Exception{
				
		List<MeetingPlaceInfo> items = DataWebService.Instance.GetMeetingPlaces();
		
		for(int i=0; i<items.size(); i++) {
			
			MeetingPlaceInfo item = items.get(i);
						
			if (MeetingPlaces.Instance.GetItem(item.GetMeetingPlaceID())==null){
				
				MeetingPlaces.Instance.addItem(item);
			}
			else
			{
				MeetingPlaces.Instance.updateItem(item);
			}
		}
	}
	
	public void SyncMeetingSessions() throws Exception{
				
		List<MeetingSessionInfo> items = DataWebService.Instance.GetMeetingSessions();
		
		for(int i=0; i<items.size(); i++) {
			
			MeetingSessionInfo item = items.get(i);
						
			if (MeetingSessions.Instance.GetItem(item.GetSessionID())==null){
				
				MeetingSessions.Instance.addItem(item);
			}
			else
			{
				MeetingSessions.Instance.updateItem(item);
			}
		}
	}
	
	public void SyncMeetingStatuses() throws Exception{
		
		List<MeetingStatusInfo> items = DataWebService.Instance.GetMeetingStatuses();
		
		for(int i=0; i<items.size(); i++) {
			
			MeetingStatusInfo item = items.get(i);
						
			if (MeetingStatuses.Instance.GetItem(item.GetMeetingStatusID())==null){
				
				MeetingStatuses.Instance.addItem(item);
			}
			else
			{
				MeetingStatuses.Instance.updateItem(item);
			}
		}
	}
	
	public void SyncMeetings() throws Exception{
		
		List<MeetingInfo> items = DataWebService.Instance.GetMeetings();
		
		for(int i=0; i<items.size(); i++) {
			
			MeetingInfo item = items.get(i);
						
			if (Meetings.Instance.GetItem(item.GetMeetingID())==null){
				
				Meetings.Instance.addItem(item);
			}
			else
			{
				Meetings.Instance.updateItem(item);
			}
		}
	}	
	
	public void SyncReplyTypes() throws Exception{
		
		List<ReplyTypeInfo> items = DataWebService.Instance.GetReplyTypes();
		
		for(int i=0; i<items.size(); i++) {
			
			ReplyTypeInfo item = items.get(i);
						
			if (ReplyTypes.Instance.GetItem(item.GetReplyTypeID())==null){
				
				ReplyTypes.Instance.addItem(item);
			}
			else
			{
				ReplyTypes.Instance.updateItem(item);
			}
		}
	}
	
	public void SyncMeetingItems() throws Exception{
		
		List<MeetingInfo> meetings= Meetings.Instance.GetAllItems();
		
		for(int j = 0; j<meetings.size(); j++) {
			
			List<MeetingItemInfo> items = DataWebService.Instance.GetMeetingItems(meetings.get(j).GetMeetingID());
			
			for(int i=0; i<items.size(); i++) {
				
				MeetingItemInfo item = items.get(i);
							
				if (MeetingItems.Instance.GetItem(item.GetMeetingItemID())==null){
					
					MeetingItems.Instance.addItem(item);
				}
				else
				{
					MeetingItems.Instance.updateItem(item);
				}
			}
		}					

	}
	
	public void SyncMeetingAttachments() throws Exception{
		
		List<MeetingInfo> meetings= Meetings.Instance.GetAllItems();
		
		for(int j = 0; j<meetings.size(); j++) {
			
			List<MeetingAttachmentInfo> items = DataWebService.Instance.GetMeetingAttachments(meetings.get(j).GetMeetingID());
			
			for(int i=0; i<items.size(); i++) {
				
				MeetingAttachmentInfo item = items.get(i);
							
				if (MeetingAttachments.Instance.GetItem(item.GetMeetingID(),item.GetAttachmentID())==null){
					
					MeetingAttachments.Instance.addItem(item);
				}
			}
		}					

	}
	
	public void SyncMeetingItemsAttachments() throws Exception{
		
		List<MeetingItemInfo> meetingItems= MeetingItems.Instance.GetAllItems();
		
		for(int j = 0; j<meetingItems.size(); j++) {
			
			List<MeetingItemsAttachmentInfo> items = DataWebService.Instance.GetMeetingItemAttachment(meetingItems.get(j).GetMeetingItemID());
			
			for(int i=0; i<items.size(); i++) {
				
				MeetingItemsAttachmentInfo item = items.get(i);
							
				if (MeetingItemsAttachments.Instance.GetItem(item.GetMeetingItemID(),item.GetAttachmentID())==null){
					
					MeetingItemsAttachments.Instance.addItem(item);
				}
			}
		}					

	}
	
	public void SyncMeetingAttendees() throws Exception{
		
		List<MeetingInfo> meetings= Meetings.Instance.GetAllItems();
		
		for(int j = 0; j<meetings.size(); j++) {
			
			List<MemberInfo> items = DataWebService.Instance.GetPresents(meetings.get(j).GetMeetingID());
			
			for(int i=0; i<items.size(); i++) {
				
				MemberInfo item = items.get(i);
							
				if (Members.Instance.GetItemByMemberID(item.GetMemberID()) ==null){
					
					Members.Instance.addItem(item);
				}
				
				MeetingAttendeeInfo meetingAttendee = MeetingAttendees.Instance.GetItem(meetings.get(j).GetMeetingID(), item.GetMemberID());
				
				if(meetingAttendee == null){
					
					MeetingAttendees.Instance.addItem(new MeetingAttendeeInfo(meetings.get(j).GetMeetingID(), item.GetMemberID(),true));
				}
				else{
					
					meetingAttendee.SetMeetingID(meetings.get(j).GetMeetingID());
					meetingAttendee.SetMemberID(item.GetMemberID());
					meetingAttendee.SetAttended(true);
					
					MeetingAttendees.Instance.updateItem(meetingAttendee);
				}
			}
			
			
			
			items = DataWebService.Instance.GetApologists(meetings.get(j).GetMeetingID());
			
			for(int i=0; i<items.size(); i++) {
				
				MemberInfo item = items.get(i);
							
				if (Members.Instance.GetItemByMemberID(item.GetMemberID()) ==null){
					
					Members.Instance.addItem(item);
				}
				
				
				MeetingAttendeeInfo meetingAttendee = MeetingAttendees.Instance.GetItem(meetings.get(j).GetMeetingID(), item.GetMemberID());
				
				if(meetingAttendee == null){
					
					MeetingAttendees.Instance.addItem(new MeetingAttendeeInfo(meetings.get(j).GetMeetingID(), item.GetMemberID(),false));
				}
				else{
					
					meetingAttendee.SetMeetingID(meetings.get(j).GetMeetingID());
					meetingAttendee.SetMemberID(item.GetMemberID());
					meetingAttendee.SetAttended(false);
					
					MeetingAttendees.Instance.updateItem(meetingAttendee);
				}
			}
		}					

	}
	
	
	/*public void SyncMeetingMemberAttachments() throws Exception{
		
		List<MeetingInfo> meetings= Meetings.Instance.GetAllItems();
		
		for(int j = 0; j<meetings.size(); j++) {
			
			List<MeetingMemberAttachmentInfo> items = DataWebService.Instance.GetMeetingMemberAttachments(meetings.get(j).GetMeetingID());
			
			for(int i=0; i<items.size(); i++) {
				
				MeetingMemberAttachmentInfo item = items.get(i);
							
				if (MeetingMembersAttachments.Instance.GetItem(item.GetMeetingID(),item.)==null){
					
					MeetingAttachments.Instance.addItem(item);
				}
			}
		}					

	}*/
	
	
	public void SyncAllData(final Context context , final Handler handler){
		
        dialog = AppDialogs.ProgressDialog(context, 0, max, "", context.getString(R.string.Synchronize));
        	
        Thread background = new Thread (new Runnable() {
            public void run() {    
            	try	{
                	SyncMeetingPlaces();
                	progressHandler.sendMessage(progressHandler.obtainMessage(Activity.RESULT_OK));
                	SyncMeetingSessions();
                	progressHandler.sendMessage(progressHandler.obtainMessage(Activity.RESULT_OK));
                	SyncMeetingStatuses();
                	progressHandler.sendMessage(progressHandler.obtainMessage(Activity.RESULT_OK));                    	
					SyncReplyTypes();		
                	progressHandler.sendMessage(progressHandler.obtainMessage(Activity.RESULT_OK));
                	SyncMeetings();
                	progressHandler.sendMessage(progressHandler.obtainMessage(Activity.RESULT_OK));
                	SyncMeetingItems();
                	progressHandler.sendMessage(progressHandler.obtainMessage(Activity.RESULT_OK));
                	SyncMeetingAttachments();
                	progressHandler.sendMessage(progressHandler.obtainMessage(Activity.RESULT_OK));
                	SyncMeetingItemsAttachments();
                	progressHandler.sendMessage(progressHandler.obtainMessage(Activity.RESULT_OK));
                	SyncMeetingAttendees();
                	progressHandler.sendMessage(progressHandler.obtainMessage(Activity.RESULT_OK));                	
                	
                	
                	if(handler!=null)
                		handler.sendMessage(handler.obtainMessage(Activity.RESULT_OK));
                }
                catch(Exception ex){
        
                	Helper.DisplayNotification(ex.toString());
                	progressHandler.sendMessage(progressHandler.obtainMessage(Activity.RESULT_CANCELED));
                	
                	if(handler!=null)
                		handler.sendMessage(handler.obtainMessage(Activity.RESULT_CANCELED));
                	
                }
            }
         });
        
        background.start();


  
         // start the background thread
        
	}
	
	  // handler for the background updating
    Handler progressHandler = new Handler() {
        public void handleMessage(Message msg) {
        	
        	if(msg.what == Activity.RESULT_OK )
        	{  
        		dialog.incrementProgressBy(increment);
        		
	            if(max<=dialog.getProgress()){            
		          	dialog.dismiss();  
		          	Helper.ShowToast(R.string.OperationSuccessfully);     			        	
	        	} 

        	} 
        	else{
	        	 	        	
        		dialog.dismiss(); 
        		Helper.ShowToast(R.string.WebServiceURLNotValid);
        	}
                       

        }
    };
	
}
